package com.nehc.nettyserver.config;

import java.util.concurrent.Executors;
import java.util.concurrent.ExecutorService;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.AsyncTaskExecutor;
import org.springframework.core.task.support.TaskExecutorAdapter;
import org.springframework.scheduling.annotation.EnableAsync;

/**
 * 线程配置类 - 启用JDK 21虚拟线程
 * 
 * @author NEHC
 * @date 2023-04-02
 */
@Configuration
@EnableAsync
public class ThreadConfig {

    /**
     * 配置Spring异步任务使用虚拟线程
     */
    @Bean
    public AsyncTaskExecutor applicationTaskExecutor() {
        // 使用无界虚拟线程执行器
        ExecutorService executorService = Executors.newVirtualThreadPerTaskExecutor();
        return new TaskExecutorAdapter(executorService);
    }
    
    /**
     * 配置用于处理HTTP请求的虚拟线程执行器
     */
    @Bean
    public ExecutorService httpRequestExecutor() {
        return Executors.newVirtualThreadPerTaskExecutor();
    }
    
    /**
     * 配置用于文件IO操作的虚拟线程执行器
     */
    @Bean
    public ExecutorService fileIoExecutor() {
        return Executors.newVirtualThreadPerTaskExecutor();
    }
} 